//  UIView+extension.swift
//
//  Created by laichunhui on 15/9/30.
//  Copyright © 2015年 lch. All rights reserved.
//  UIView扩展

import UIKit

/* 对frame的扩展 */
internal extension UIView {
   
    // X值
    var x: CGFloat
    {
        get
        {
            return self.frame.origin.x
    
        }
        set (newX)
        {
            self.frame.origin.x = newX
        }
    }
    // Y值
    var y: CGFloat
    {
        get
        {
            return self.frame.origin.y
            
        }
        set (newY)
        {
            self.frame.origin.y = newY
        }
    }
    // centerX
    var centerX: CGFloat
    {
        get
        {
            return self.center.x
            
        }
        set (newCenterX)
        {
            self.center.x = newCenterX
        }
    }
    // centerY
    var centerY: CGFloat
    {
        get
        {
            return self.center.y
            
        }
        set (newCenterY)
        {
            self.center.y = newCenterY
        }
    }
    // 宽度
    var width: CGFloat
    {
        get
        {
            return self.frame.size.width
            
        }
        set (newWidth)
        {
            self.frame.size.width = newWidth
        }
    }
    // 高度
    var height: CGFloat
    {
        get
        {
            return self.frame.size.height
            
        }
        set (newHeight)
        {
            self.frame.size.height = newHeight
        }
        
    }
    // size
    var size: CGSize
    {
        get
        {
            return self.frame.size
            
        }
        set (newSize)
        {
            self.frame.size = newSize
        }
        
    }
    // origin
    var origin: CGPoint
    {
        get
        {
            return self.frame.origin
            
        }
        set (newOrigin)
        {
            self.frame.origin = newOrigin
        }
    }
    /**
        判断view是否在屏幕显示
    */
    func isShowingOnKeyWindow() ->Bool
    {
        // 主窗口
        let keyWindow = UIApplication.sharedApplication().keyWindow
        
        // 以主窗口左上角为坐标原点, 计算self的矩形框
        let newFrame = keyWindow?.convertRect(self.frame, fromView: self.superview)
        let winBounds = keyWindow?.bounds
        
        // 主窗口的bounds 和 self的矩形框 是否有重叠
        let intersects:Bool = CGRectIntersectsRect(newFrame!, winBounds!)
        
        return !self.hidden && self.alpha > 0.1 && self.window == keyWindow && intersects == true
    }
    
//    class func viewFromNib() -> UIView {
//        self	UIView.Type	MyWeiBlog.MeTableHeaderView
//        return NSBundle.mainBundle().loadNibNamed(NSStringFromClass(self), owner: nil, options: nil).last as! UIView
//    }
    
}